<?xml version="1.0" ?>

<xsl:stylesheet   xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
						xmlns:lzc="http://schemas.icitadel.com/Lazarus/Compiled.xsd"
                  version="1.0">

   <xsl:output method="text"/>

	<xsl:include href="../../common.xsl" />
	<xsl:include href="../../convert.xsl" />
   
   <xsl:template match="lzc:project"><xsl:apply-templates select="lzc:tables/lzc:table[ ( @compiling ) ]" /></xsl:template>

   <xsl:template match="lzc:table[ @compiling ]">
			<xsl:variable name="v_ObjectName">
				<xsl:value-of select="@name" />
			</xsl:variable>
			<xsl:variable name="v_ObjectLogicalName">
				<xsl:value-of select="@logical-name" />
			</xsl:variable>
         <xsl:variable name="v_ClassName">
            <xsl:value-of select="@name" />
			 <xsl:text>Base</xsl:text>
         </xsl:variable>
         <xsl:variable name="v_collections" select="lzc:collections/lzc:collection" />
         <xsl:variable name="v_NamespacePrefix">
				<xsl:if test="/lzc:project/@namespaceprefix">
					<xsl:value-of select="/lzc:project/@namespaceprefix" />
					<xsl:text>:</xsl:text>
				</xsl:if>
         </xsl:variable>
         <xsl:variable name="v_RuntimeNamespace">
				<xsl:if test="ancestor::lzc:project/@defaultnamespace">
					<xsl:value-of select="ancestor::lzc:project/@defaultnamespace"/>
				</xsl:if>
         </xsl:variable>
         <xsl:variable name="v_BusinessConceptNamespace">
				<xsl:if test="/lzc:project/@businessconcept">
					<xsl:text>.</xsl:text>
					<xsl:value-of select="/lzc:project/@businessconcept" />
				</xsl:if>
         </xsl:variable>///***********************************************************************
/// <xsl:value-of select="$v_ClassName" />.cs
/// Owner: joemu
///
/// Provides a .NET Wrapper around the Stored Procedures in the
/// <xsl:value-of select="ancestor::lzc:project/@name" /> database.
///
/// Copyright(c) Microsoft Corporation
///***********************************************************************

using System;
using System.Data;
using System.Data.SqlClient;
using System.Xml;

using OR.Reuse.Persistence;

using <xsl:value-of select="$v_RuntimeNamespace" />
	   <xsl:text>.DataAccess;
	   
namespace </xsl:text>
	<xsl:value-of select="$v_RuntimeNamespace" />
	<xsl:text>.DataAccess</xsl:text>
	<xsl:value-of select="$v_BusinessConceptNamespace" />
	<xsl:text>.Generated
{
   /// &lt;summary&gt;
   /// </xsl:text><xsl:value-of select="lzc:description" /><xsl:text>
   /// &lt;/summary&gt;
   public class </xsl:text><xsl:value-of select="$v_ClassName" /> : DataAccessBase
   {
      #region Private Property Fields
      
      #endregion
      
      #region Constructors
      
      /// &lt;summary&gt;
	  /// Constructs a new <xsl:value-of select="@friendly-name" /> and initializes a new SqlConnection.
      /// &lt;/summary&gt;
      public <xsl:value-of select="$v_ClassName" />() : base()
      {
      }
      
      /// &lt;summary&gt;
      /// Constructs a new <xsl:value-of select="@friendly-name" /> initialized with the specified SqlConnection.
      /// &lt;/summary&gt;
      public <xsl:value-of select="$v_ClassName" />( SqlConnection existingConnection) : base( existingConnection )
      {
      
      }
      
      #endregion
      
      #region Public Methods

      <xsl:apply-templates select="lzc:methods/lzc:method" />

	   public override DataTable EmptyTable()
	   {
	   DataTable rtnVal = new DataTable();

	   rtnVal.TableName = "<xsl:value-of select="@name" />";
         
         <xsl:for-each select="lzc:fields/lzc:field">
			 rtnVal.Columns.Add( new DataColumn( "<xsl:value-of select="@dbname" />", typeof( <xsl:call-template name="getNETType"><xsl:with-param name="pFieldType" select="@type" /></xsl:call-template> ) ) );</xsl:for-each>

	   return( rtnVal );
	   }

	   #endregion
	}
}
   </xsl:template>
   
   <xsl:template match="lzc:method[ ( @methodtype = 'Create' ) or ( @methodtype = 'Update' ) or ( ( @methodtype = 'Custom' and ( @return-result ) ) ) ]">
      <xsl:variable name="all_fields" select="lzc:method-params/lzc:method-param" />
      /// &lt;summary&gt;
      /// <xsl:value-of select="lzc:description" />
      /// &lt;/summary&gt;
      <xsl:for-each select="$all_fields">
      <xsl:text>/// &lt;param name="</xsl:text>
      <xsl:value-of select="@dbname" />
         <xsl:text>"&gt;</xsl:text>
         <xsl:value-of select="lzc:description"/>
         <xsl:text>&lt;/param&gt;
      </xsl:text>
      </xsl:for-each>public void <xsl:value-of select="@object-name" />
      <xsl:call-template name="createParams">
         <xsl:with-param name="fields" select="$all_fields" />
      </xsl:call-template>
      {
         <xsl:call-template name="CreateSqlCommand">
            <xsl:with-param name="CommandText">[sp<xsl:value-of select="@db-name" />]</xsl:with-param>
         </xsl:call-template><xsl:text>

         currentCommand.Parameters.Add( new SqlParameter( "@RETURN_VALUE", SqlDbType.Int, 4, ParameterDirection.ReturnValue, false, ((System.Byte)(10)), ((System.Byte)(0)), "", System.Data.DataRowVersion.Current, null ) );
         </xsl:text>
         <xsl:call-template name="createSqlCommandParams">
            <xsl:with-param name="fields" select="$all_fields" />
         </xsl:call-template>
      
	     if ( Connection.State != ConnectionState.Open ) { Connection.Open(); }
         
         currentCommand.ExecuteNonQuery();
         
         // Dump out the output params.
         <xsl:for-each select="lzc:method-params/lzc:method-param[ @direction = 'out' ]">
            <xsl:variable name="ParameterName">
               <xsl:text>@</xsl:text>
               <xsl:if test="ancestor::lzc:project/use-parameter-prefix">p</xsl:if>
               <xsl:value-of select="@dbname" />
            </xsl:variable>
				<xsl:value-of select="@dbname"/>
				<xsl:text> = ( </xsl:text>
				<xsl:call-template name="getNETParamType">
					<xsl:with-param name="pFieldType" select="@type" />
					<xsl:with-param name="pRequired" select="@required" />
				</xsl:call-template>
				<xsl:text> )currentCommand.Parameters[ "</xsl:text>
				<xsl:value-of select="$ParameterName" />
				<xsl:text>" ].Value;</xsl:text>
         </xsl:for-each>
      
	     if ( !this.MaintainConnection ) { this.Connection.Close(); }
      }
   </xsl:template>
   
   <xsl:template match="lzc:method[ ( @methodtype = 'Get' ) or ( @methodtype = 'List' ) or ( ( @methodtype = 'Custom' and ( @return-result ) ) ) ]">
      <xsl:variable name="all_fields" select="lzc:method-params/lzc:method-param" />
      <xsl:variable name="v_ClassName">
         <xsl:value-of select="ancestor::lzc:table/@name" />
      </xsl:variable>
      /// &lt;summary&gt;
	  /// <xsl:value-of select="lzc:description" />
	  /// &lt;/summary&gt;
      <xsl:for-each select="$all_fields">
      <xsl:text>/// &lt;param name="</xsl:text><xsl:value-of select="@dbname" />
         <xsl:text>"&gt;</xsl:text>
         <xsl:value-of select="lzc:description"/>
         <xsl:text>&lt;/param&gt;
      </xsl:text>
      <xsl:text>/// &lt;returns&gt;DataSet containing matching records.&lt;/returns&gt;
      </xsl:text>
      </xsl:for-each>
      <xsl:text>public </xsl:text>
      <xsl:if test="( @object-name = 'Get' )">override </xsl:if> 
      <xsl:text>DataSet </xsl:text><xsl:value-of select="@object-name" />
      <xsl:call-template name="createParams">
         <xsl:with-param name="fields" select="$all_fields" />
      </xsl:call-template>
      {
         try
         {
				<xsl:call-template name="CreateSqlCommand">
					<xsl:with-param name="CommandText">[sp<xsl:value-of select="@db-name" />]</xsl:with-param>
				</xsl:call-template>

				currentCommand.Parameters.Add( new SqlParameter( "@RETURN_VALUE", SqlDbType.Int, 4, ParameterDirection.ReturnValue, false, ((System.Byte)(10)), ((System.Byte)(0)), "", System.Data.DataRowVersion.Current, null ) );
				<xsl:call-template name="createSqlCommandParams">
					<xsl:with-param name="fields" select="$all_fields" />
				</xsl:call-template>
      
			if ( this.Connection.State != ConnectionState.Open ) { this.Connection.Open(); }
            
				SqlDataReader resultReader = currentCommand.ExecuteReader();
	         
				// Dump out the output params.
				<xsl:for-each select="lzc:method-params/lzc:method-param[ @direction = 'out' ]">
               <xsl:variable name="ParameterName">
                  <xsl:text>@</xsl:text>
                  <xsl:if test="ancestor::lzc:project/use-parameter-prefix">p</xsl:if>
                  <xsl:value-of select="@dbname" />
               </xsl:variable>
               <xsl:value-of select="@dbname"/>
					<xsl:text> = ( </xsl:text>
					<xsl:call-template name="getNETParamType">
						<xsl:with-param name="pFieldType" select="@type" />
						<xsl:with-param name="pRequired" select="True" />
					</xsl:call-template>
					<xsl:text> )currentCommand.Parameters[ "</xsl:text>
					<xsl:value-of select="$ParameterName" />
					<xsl:text>" ].Value;</xsl:text>
				</xsl:for-each>
            
            DataSet rtnVal = new DataSet();
            rtnVal.Load( resultReader, LoadOption.OverwriteChanges, "<xsl:value-of select="$v_ClassName" />" );
            
            resultReader.Close();
            
	     if ( !this.MaintainConnection ) { this.Connection.Close(); }
            
            return( rtnVal );
         }
         catch ( SqlException e )
         {
            throw new Exception ( e.Message ); 
         }         
      }
   </xsl:template>
   
   <xsl:template name="createSqlCommandParams">
      <xsl:param name="fields" />
      <xsl:for-each select="$fields">
         <xsl:variable name="ParameterName">
            <xsl:text>@</xsl:text>
            <xsl:if test="ancestor::lzc:project/use-parameter-prefix">p</xsl:if>
            <xsl:value-of select="@dbname" />
         </xsl:variable>
         <xsl:choose>
            <xsl:when test="not( @required = 'True' ) and ( @direction = 'in' )">
               <xsl:choose>
                  <xsl:when test="@type = $DATATYPE_GUID">
                     <xsl:text>if ( </xsl:text><xsl:value-of select="@dbname" /><xsl:text> == Guid.Empty )</xsl:text>
                  </xsl:when>
                  <xsl:when test="((@type = $DATATYPE_VARCHAR) or (@type = $DATATYPE_TEXT))">
							<xsl:text>if ( String.IsNullOrEmpty( </xsl:text><xsl:value-of select="@dbname" /><xsl:text> ) )</xsl:text>
                  </xsl:when>
                  <xsl:otherwise>
                     <xsl:text>if ( !</xsl:text><xsl:value-of select="@dbname" /><xsl:text>.HasValue )</xsl:text>
                  </xsl:otherwise>
               </xsl:choose><xsl:text>
         {
            currentCommand.Parameters.Add( new SqlParameter( "</xsl:text>
               <xsl:value-of select="$ParameterName"/>
                  <xsl:text>", </xsl:text>
                  <xsl:call-template name="getSqlDbType">
                     <xsl:with-param name="pFieldType" select="@type" />
                  </xsl:call-template>
                  <xsl:if test="not( @type = $DATATYPE_TEXT )">
                     <xsl:text>, </xsl:text>
                     <xsl:call-template name="getSqlDbSize">
                        <xsl:with-param name="pFieldType" select="@type" />
                        <xsl:with-param name="pFieldSize" select="@size" />
                     </xsl:call-template>
                  </xsl:if>
                  <xsl:text> ) )</xsl:text>
                  <xsl:if test="@direction = 'in'">
                     <xsl:text>.Value = DBNull.Value</xsl:text>
                  </xsl:if>
                  <xsl:text>;</xsl:text>
         }
         else
         {
            <xsl:text>currentCommand.Parameters.Add( new SqlParameter( "</xsl:text>
                  <xsl:value-of select="$ParameterName" />
                  <xsl:text>", </xsl:text>
                  <xsl:call-template name="getSqlDbType">
                     <xsl:with-param name="pFieldType" select="@type" />
                  </xsl:call-template>
                  <xsl:if test="not( @type = $DATATYPE_TEXT )">
                     <xsl:text>, </xsl:text>
                     <xsl:call-template name="getSqlDbSize">
                        <xsl:with-param name="pFieldType" select="@type" />
                        <xsl:with-param name="pFieldSize" select="@size" />
                     </xsl:call-template>
                  </xsl:if>
                  <xsl:text> ) )</xsl:text>
                  <xsl:if test="@direction = 'in'">
                     <xsl:text>.Value = </xsl:text>
                     <xsl:choose>
                        <xsl:when test="@type = $DATATYPE_INT">
                           <xsl:value-of select="@dbname" />
                           <xsl:text>.Value</xsl:text>
                        </xsl:when>
                        <xsl:when test="@type = $DATATYPE_DATETIME">
                           <xsl:value-of select="@dbname" />
                           <xsl:text>.Value</xsl:text>
                        </xsl:when>
								<xsl:otherwise>
									<xsl:text></xsl:text>
									<xsl:value-of select="@dbname" />
								</xsl:otherwise>
                     </xsl:choose>
                  </xsl:if>
                  <xsl:text>;</xsl:text>
            }

            </xsl:when>
            <xsl:otherwise>
               <xsl:text>currentCommand.Parameters.Add( new SqlParameter( "</xsl:text>
               <xsl:value-of select="$ParameterName" />
               <xsl:text>", </xsl:text>
               <xsl:call-template name="getSqlDbType">
                  <xsl:with-param name="pFieldType" select="@type" />
               </xsl:call-template>
               <xsl:if test="not( @type = $DATATYPE_TEXT )">
                  <xsl:text>, </xsl:text>
                  <xsl:call-template name="getSqlDbSize">
                     <xsl:with-param name="pFieldType" select="@type" />
                     <xsl:with-param name="pFieldSize" select="@size" />
                  </xsl:call-template>
               </xsl:if>
               <xsl:text> ) )</xsl:text>
               <xsl:if test="@direction = 'in'">
                  <xsl:text>.Value = </xsl:text>
                  <xsl:choose>
                     <xsl:when test="@type = $DATATYPE_INT">
                        <xsl:value-of select="@dbname" />
                     </xsl:when>
                     <xsl:when test="@type = $DATATYPE_DATETIME">
                        <xsl:value-of select="@dbname" />
                     </xsl:when>
                     <xsl:otherwise>
						<xsl:value-of select="@dbname" />
                     </xsl:otherwise>
                  </xsl:choose>
               </xsl:if>
               <xsl:text>;
         </xsl:text>
               <xsl:if test="@direction = 'out'"><xsl:text>
				currentCommand.Parameters[ "</xsl:text><xsl:value-of select="$ParameterName" /><xsl:text>" ].Direction = ParameterDirection.Output;
            </xsl:text>
               </xsl:if>
            </xsl:otherwise>
         </xsl:choose>
      </xsl:for-each>
   </xsl:template>
   
   <xsl:template name="createParams">
      <xsl:param name="fields" />
      <xsl:choose>
         <xsl:when test="count( $fields ) &gt; 0">
      (
         <xsl:for-each select="$fields">
         <xsl:if test="@direction = 'out'">out </xsl:if>
         <xsl:call-template name="getNETParamType">
				<xsl:with-param name="pFieldType" select="@type" />
				<xsl:with-param name="pRequired" select="@required" />
			</xsl:call-template>
			<xsl:value-of select="$space" />
         <xsl:value-of select="@dbname" />
         <xsl:if test="position() &lt; count( $fields )">,
         </xsl:if>
         </xsl:for-each>
      )</xsl:when>
         <xsl:when test="count( $fields ) = 0">()</xsl:when>
      </xsl:choose>
   </xsl:template>

   <xsl:template name="CreateSqlCommand">
      <xsl:param name="CommandText" />
         SqlCommand  currentCommand      = new SqlCommand();
         
         currentCommand.CommandType      = CommandType.StoredProcedure;
         currentCommand.CommandText      = "<xsl:value-of select="$CommandText" />";
         currentCommand.Connection       = this.Connection;
   </xsl:template>

</xsl:stylesheet>